package com.uniapp.server.controller;

import com.uniapp.server.common.R;
import com.uniapp.server.model.RecordEntity;
import com.uniapp.server.service.RecordService;
import com.uniapp.server.vo.RecordByTimeRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

/**
 * @author cyt
 * @describe
 * @create 2021-01-15 23:46
 */
@RequestMapping("/api/record")
@RestController
public class RecordController {

    @Autowired
    private RecordService recordService;

    @PostMapping("/getByTime")
    public R getByCreateTime(@RequestBody RecordByTimeRequest recordByTimeRequest) {
        ArrayList<RecordEntity> time = recordService.getByCreateTime(recordByTimeRequest.getTime());
        return R.ok().data("time", time);
    }

    @PutMapping("/update")
    public R update(@RequestBody RecordEntity recordEntity) {
        recordService.update(recordEntity);
        return R.ok();
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/getPage/{page}/{size}")
    public R query(@PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Page<RecordEntity> servicePage = recordService.getPage(page, size);
        return R.ok().data("page", servicePage);
    }

    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable Integer id) {
        RecordEntity byId = recordService.getById(id);
        return R.ok().data("item", byId);
    }


    @GetMapping("/{id}")
    public R getById(@PathVariable Integer id) {
        RecordEntity byId = recordService.getById(id);
        return R.ok().data("item", byId);
    }


    @PostMapping("/add")
    public R add(@RequestBody RecordEntity recordEntity) {
        recordService.add(recordEntity);
        return R.ok();
    }
}
